package com.cyandpos.pad.activity.attachfunction;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.BaseActivity;
import com.cyandpos.pad.attachselect.presenter.IMemberPresenter;
import com.cyandpos.pad.attachselect.presenter.MemberPresenterImpl;
import com.cyandpos.pad.attachselect.view.IMemberView;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.GetValueCardResponse;
import com.cyandpos.pad.posdata.GetVipCouponResponse;
import com.cyandpos.pad.posdata.QueryMemberInfoResponse;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.PayUtil;
import com.cyandpos.pad.utils.ToastUtils;
import com.cyandpos.pad.utils.view.InputNumberDialogCommon;

import butterknife.BindView;

public class ValueCardActivity extends BaseActivity implements IMemberView, View.OnClickListener {
    public static AlertDialog mDialog;//所有dialog
    private static IMemberPresenter mMemberPresenterImpl;
    private ProgressDialog progressDialog;

    @BindView(R.id.tv_cardno)
    TextView tvCardno;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_dialog_title_name)
    TextView tvtitle_name;
    @Override
    protected void initData() {
        mMemberPresenterImpl = new MemberPresenterImpl(this);
        progressDialog = new ProgressDialog(ValueCardActivity.this);
        showQueryMemberDialog(ValueCardActivity.this,"储值卡余额");
        tvtitle_name.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_value_card;
    }
    public  void showQueryMemberDialog(final Activity mContext, final String flag) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_member, null);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.member_soft_keyboard);

        boolean isNeedInputView = false;//是否需要输入布局

        tvName.setText(flag);
        //关闭按钮
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });

        view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_card).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.GONE);

        if (flag.equals("储值卡余额")) {

            view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_card).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.VISIBLE);

            isNeedInputView = true;

        }

        if(!isNeedInputView)
            sf_containt.setVisibility(View.GONE);//不需要输入布局

        //储值卡 刷卡
        view.findViewById(R.id.tv_sale_member_swipe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();


            }
        });
        //储值卡 扫码
        view.findViewById(R.id.tv_sale_member_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();

            }
        });
        //卡号
        final EditText etCardNo = view.findViewById(R.id.et_sale_member_card);
        final TextView tvCardNo = view.findViewById(R.id.tv_sale_member_card);

        //手机号
        final EditText etPhone = view.findViewById(R.id.et_sale_member_phone);
        final TextView tvPhone = view.findViewById(R.id.tv_sale_member_phone);

        tvCardNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tvCardNo.setVisibility(View.GONE);
                etCardNo.setVisibility(View.VISIBLE);

                tvPhone.setVisibility(View.VISIBLE);
                etPhone.setVisibility(View.GONE);

                etPhone.setText("");
                etCardNo.setFocusable(true);
                sf_containt.setEnterReceptor(etCardNo);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("卡号开始查询储值卡");
                        Log.i("TAG----NUM:",num);


                        showVerifiedCodeDialog(num);
                    }
                });
            }
        });
        CommonUtils.setNoSoftware(etCardNo, mContext,true);
        CommonUtils.setNoSoftware(etPhone, mContext,true);

        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvPhone.setVisibility(View.GONE);
                etPhone.setVisibility(View.VISIBLE);

                tvCardNo.setVisibility(View.VISIBLE);
                etCardNo.setVisibility(View.GONE);

                etCardNo.setText("");
                etPhone.setFocusable(true);
                sf_containt.setEnterReceptor(etPhone);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        Log.i("TAG----手机号NUM:",num);

                        ToastUtils.show("手机号开始查询储值卡");
                        Log.i("TAG----手机号NUM:",num);

                        queryValueCard(num,3,"");

                    }
                });

            }
        });
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }


    public static void queryValueCard(String content, int type, String verifyCode) {
        QueryMemberRequest requestParam = new QueryMemberRequest();
        requestParam.setCondType(type);
        requestParam.setCondValue(content);
        requestParam.setVerifyCode(verifyCode);
        mMemberPresenterImpl.queryValueCard(requestParam);
    }

    @Override
    public void showMemberDetail(QueryMemberInfoResponse member) {

    }

    @Override
    public void showCouponDetail(GetVipCouponResponse GetVipCouponResponseData) {

    }

    @Override
    public void showCentDetail(QueryMemberInfoResponse member) {

    }

    @Override
    public void showValueCard(GetValueCardResponse responseData) { // 储值卡的返回
        tvCardno.setText("卡号：" + responseData.getCardCode());
        tvBalance.setText("余额：" + responseData.getBalance());

    }

    @Override
    public void showWaitDialog(String content) {
        if (progressDialog != null) {
            progressDialog.setMessage(content);
            if (!progressDialog.isShowing())
                progressDialog.show();
        }
    }

    @Override
    public void showMessage(String content) {
        ToastUtils.showMsg(content,1);

    }

    @Override
    public void dismissWaitDialog() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }
    @Override
    public void onFail(String content, int handleId) {
        ToastUtils.showMsg(content, 0);
    }

    @Override
    public void onFail(String content) {
        ToastUtils.showMsg(content, 0);

    }

    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.tv_dialog_title_name:
                   finish();
                 break;
                 default:
                     break;
         }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvtitle_name.setText("储值卡信息");
    }

    private  void showVerifiedCodeDialog(final String conValue) {
        InputNumberDialogCommon inputNumberDialogCommon= InputNumberDialogCommon.getInputNumberDialogCommon(ValueCardActivity.this, new InputNumberDialogCommon.InputDialogOnSubmitClickListener() {
            @Override
            public void InputDialogOnSubmitClick(String content) {
                if(content.equals("") || content==null){
                    ToastUtils.showMsg( "请输入储值卡验证码");
                    return;
                }
                if(content.length() >0 ){
                    queryValueCard(conValue,0,content); //储值卡号是0
                }else{
                    ToastUtils.showMsg( "输入验证码错误！");
                }
            }
        });
        inputNumberDialogCommon.setIsCanInputPoint(false);
        inputNumberDialogCommon.setTitle("储值卡验证码");
        inputNumberDialogCommon.setContent("请输入储值卡验证码");
        inputNumberDialogCommon.setEtHint("请输入储值卡验证码");
        inputNumberDialogCommon.showDialog(false);
    }

}
